package chop9demo;

import java.util.*;

/**
 * @author: muxiongxiong
 * @date: 2021年07月14日 14:21
 * 公众号：雄雄的小课堂
 * 博客：https://blog.csdn.net/qq_34137397
 * 个人站：http://www.穆雄雄.com
 * 个人站：http://www.muxiongxiong.cn
 * @Description: 类的描述
 */
public class TestList {
    public static void main(String[] args) {
        iter();
    }

    //给集合中添加人对象
    static void testAddP() {
        People people = new People("王炎霸", 12, "男");
        People people1 = new People("范啸天", 14, "女");
        People people2 = new People("华神通", 15, "男");
        List list = new ArrayList();
        list.add(people);
        list.add(people1);
        list.add(people2);


        for (Object o : list) {
            People p = (People) o;
            System.out.println(p.getName());
        }

    }

    //泛型集合，约束集合中的元素
    static void FanList() {
        List<People> strList = new ArrayList<People>();
        People people = new People("王炎霸", 12, "男");
        People people1 = new People("炎王霸", 15, "女");
        People people2 = new People("范啸天", 34, "男");

        strList.add(people);
        strList.add(people1);
        strList.add(people2);

        for (People p : strList) {
            System.out.println(p.getName());
        }
    }

    //Map接口，键值对映射关系
    static void TestMap() {
        Map map = new HashMap();
        map.put("cn", "中国");
        map.put("en", "英国");
        map.put("us", "美国");
        map.put("e", "俄罗斯");

        //根据键来取值
        System.out.println("根据键来获取值：" + map.get("cn"));
        //集合的长度
        System.out.println("一共有：" + map.size() + "个元素");
        //把美国移除掉
        map.remove("us");
        //获取所有的值
        for (Object o : map.values()) {
            System.out.println("国家是：" + o);
        }
        //获取所有的键
        for (Object o : map.keySet()) {
            System.out.println("键是：" + o);
        }
        //判断是否包含en这个键
        if (map.containsKey("en")) {
            System.out.println("包含en这个键");
        }

    }

    //泛型Map集合
    static void FanMap() {
        Map<Integer, String> mapList = new HashMap<Integer, String>();
        mapList.put(1, "壹");
        mapList.put(2, "贰");
        mapList.put(3, "叁");
        mapList.put(4, "肆");
        //根据键遍历所有的值
        for (Integer str : mapList.keySet()) {
            System.out.println(mapList.get(str));
        }
    }

    //迭代器
    static void iter() {
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println("for循环遍历集合：");
        for (Integer i : list) {
            System.out.println(i);
        }
        System.out.println("使用迭代器来遍历：");
        Iterator i = list.iterator();
        while (i.hasNext()) {
            System.out.println(i.next());
        }
    }
}
